package com.wl.jetpackdemo.chat.emoji

import org.xml.sax.Attributes
import org.xml.sax.SAXException
import org.xml.sax.helpers.DefaultHandler

/**
 *
 * Created by wangliang on 2021/2/20.
 */
class XmlEmojiHandler : DefaultHandler() {
    private var list: MutableList<Emoticon>? = null
    private var emoticon: Emoticon? = null
    private var curTag: String? = null
    private var parent: String? = null

    public fun getList(): List<Emoticon>? {
        return list
    }

    @Throws(SAXException::class)
    override fun startDocument() {
        super.startDocument()
        list = ArrayList()
    }

    @Throws(SAXException::class)
    override fun startElement(uri: String?, localName: String, qName: String?,
                              attributes: Attributes) {
        if ("Emoticon" == localName) {
            emoticon = Emoticon()
            emoticon!!.id = attributes.getValue(0)
            emoticon!!.tag = attributes.getValue(1)
            emoticon!!.filename = attributes.getValue(2)
        } else if ("Catalog" == localName) {
            // 设置当前父目录
            parent = attributes.getValue(0)
        }
        curTag = localName
        super.startElement(uri, localName, qName, attributes)
    }

    @Throws(SAXException::class)
    override fun endElement(uri: String?, localName: String, qName: String?) {
        if ("Emoticon" == localName) {
            emoticon!!.parent = parent
            list!!.add(emoticon!!)
            emoticon = null
        } else if ("Catalog" == localName) {
            // 设置当前父目录
            parent = null
            curTag = null
        }
        super.endElement(uri, localName, qName)
    }

    @Throws(SAXException::class)
    override fun characters(ch: CharArray?, start: Int, length: Int) {
        super.characters(ch, start, length)
    }
}